package com.rs.portal.doctor.ivr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.jaxplat.commons.Row;
import com.jaxplat.commons.RowImpl;
import com.rs.commons.RsConstant;
import com.rs.commons.delegate.CommonDelegate;
import com.rs.commons.web.PortalBaseAction;
import com.rs.rsbiz.doctor.delegate.DoctorDelegate;
import com.rs.util.Utils;

public class DoctorToIvrAction extends PortalBaseAction
{
    private DoctorDelegate doctorDelegate;

    private CommonDelegate commonDelegate;

    public CommonDelegate getCommonDelegate()
    {
        return commonDelegate;
    }

    public void setCommonDelegate(CommonDelegate commonDelegate)
    {
        this.commonDelegate = commonDelegate;
    }

    public DoctorDelegate getDoctorDelegate()
    {
        return doctorDelegate;
    }

    public void setDoctorDelegate(DoctorDelegate doctorDelegate)
    {
        this.doctorDelegate = doctorDelegate;
    }

    /**
     * 查询第一步 初始化医院信息
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView initSearch_s1(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        // List hosList = commonDelegate.getAllHospitalList();
        this.logger.debug("aaa");
        this.logger.info("bbb");
        Map attrs = new HashMap();
        List hosList = commonDelegate.getAllHospitalList();

        attrs.put("list", hosList); //
        // 这一句相当于request.setAttribute("list",list);
        // attrs.put("command", new DoctorEntity());
        // attrs.put("curDate", Utils.getCurDate("yyyy-MM-dd"));
        return new ModelAndView("doctor_search_s1", attrs);

    }

    /**
     * 初始化查询页面
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView initSearch_s2(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        // List hosList = commonDelegate.getAllHospitalList();

        Map attrs = new HashMap();
        // 设置下拉框的值
        Row param = this.parseRequestParametersToRow(request);
        initOptions(param, attrs);
        attrs.put("hospital_name", new String(param.getString("h_name").getBytes("ISO8859-1"), "UTF-8"));
        attrs.put("hospital_id", param.getString("hospital_id"));
        // attrs.put("hosOptionsList", hosList); //
        // 这一句相当于request.setAttribute("list",list);
        // attrs.put("command", new DoctorEntity());
        // attrs.put("curDate", Utils.getCurDate("yyyy-MM-dd"));
        return new ModelAndView("doctor_search", attrs);

    }

    private void initOptions(Row params, Map attrs) throws Exception
    {
        // List hosList = commonDelegate.getAllHospitalList();
        List secList = commonDelegate.getAllSectionWithParentList(params);
        String dateStr = Utils.getCurAfterDate("yyyy-MM-dd", 1);
        String hId = "";
        String sId = "";
        if (!Utils.isNullOrBlank(params.getString("hospital_id")))
            hId = params.getString("hospital_id");
        if (!Utils.isNullOrBlank(params.getString("section_id")))
            sId = params.getString("section_id");
        // String hosOptionsHtml = Utils.createOptionsHtml(hosList, "id",
        // "name", hId, "", "所有医院");
        String secOptionsHtml = Utils.createOptionsHtml(secList, "id", "ps_name", sId, "", "所有科室", true);
        attrs.put("secOptions", secOptionsHtml);
        // attrs.put("hosOptions", hosOptionsHtml); //
        // 这一句相当于request.setAttribute("list",list);
        if (!Utils.isNullOrBlank(params.getString("date")))
        {
            dateStr = params.getString("date");
        }
        // 创建日期列表
        List dateList = new ArrayList();
        Row row = null;
        for (int i = 1; i < RsConstant.PRE_SCHEDULE_DAYS; i++)
        {
            row = new RowImpl();
            row.addColumn("key", Utils.getCurAfterDate("yyyy-MM-dd", i));
            row.addColumn("val", Utils.getCurAfterDate("yyyy-MM-dd", i));
            dateList.add(row);
        }
        String dateOptionsHtml = Utils.createOptionsHtml(dateList, "key", "val", dateStr, "", "", false);
        attrs.put("dateOptions", dateOptionsHtml);
        attrs.put("date", dateStr);
    }

    /**
     * 专家查询
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView searchDoc(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);
        Map attrs = new HashMap();
        // 设置下拉框的值
        initOptions(param, attrs);
        param.put("pageSize", "100");
        param.put("pageIndex", "1");
        Object[] result = this.getDoctorDelegate().getDocList(param);
        List list = (List) result[0];

        attrs.put("list", list); // 这一句相当于request.setAttribute("list",list);
        attrs.put("hospital_name", param.getString("hospital_name"));
        attrs.put("hospital_id", param.getString("hospital_id"));
        attrs.put("name", param.getString("name"));
        return new ModelAndView("doctor_search", attrs);
    }

    /**
     * 专家详情查询
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView searchDocInfo(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);
        Map attrs = new HashMap();
        Row docInfo = this.getDoctorDelegate().getDocInfo(param);

        attrs.put("docInfo", docInfo); // 这一句相当于request.setAttribute("list",list);
        return new ModelAndView("doctor", attrs);
    }
}
